import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sptapp/api/interface/home_api.dart';
import 'package:sptapp/moudle/common_info.dart';

import '../../api/interface/user_api.dart';
import '../../api/request/response_data.dart';
import '../../config/const.dart';
import '../../widget/common_widget.dart';

class MinChooseCityPage extends StatefulWidget {
  final Map arguments;
  const MinChooseCityPage({super.key, required this.arguments});

  @override
  State<MinChooseCityPage> createState() => _MinChooseCityPageState();
}

class _MinChooseCityPageState extends State<MinChooseCityPage> {

  List states = [];
  String cityId = '';
  late Map state;
  Map? city;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    state = widget.arguments;
    if(state['cityId'] != null){
      cityId = state['cityId'].toString();
    }
    _getStates();
  }

  _getStates() async {
    showLoading();
    ResponseData res = await UserAPI.deviceCitiesAddress({
      'stateId':state['id'],
      'citiesName':''
    });
    hideLoading();
    if(res.code == 200){
      states = res.data??[];
    }
    if(mounted){
      setState(() {});
    }
  }

  _saveLocation()async{

    Map<String,dynamic> data = {
      'stateId': state['id'],
      'cityId': city?['id']??'',
      'sn':baseDeviceNum
    };
    showLoading();
    ResponseData res = await HomeAPI.deviceSetName(data);
    hideLoading();
    if(res.code == 200){
      Navigator.of(context).pop();
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,
      appBar: myAppBar(context, 'Region'),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 40.w),
        child: Column(
          children: [
            SizedBox(height: 40.w,),
            Expanded(child: Container(
              padding: EdgeInsets.symmetric(horizontal: 40.w),
              decoration: BoxDecoration(
                  color: Color(0xff1e1d20),
                  borderRadius: BorderRadius.circular(16.w)
              ),
              child: ListView.builder(itemBuilder: (ctx,index){
                Map item = states[index];
                return GestureDetector(
                  onTap: (){
                    setState(() {
                      cityId = item['id'].toString();
                    });
                    city = item;
                    _saveLocation();
                  },
                  child: Container(
                    height: 116.w,
                    decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1),width: index == 9?0: 1.w))
                    ),
                    child: Row(
                      children: [
                        Expanded(child: Text(item['city']??'',style: TextStyle(
                            color: Colors.white,
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w600
                        ),textAlign: TextAlign.left,overflow: TextOverflow.ellipsis,maxLines: 1,)),
                        SizedBox(width: 24.w,),
                        Visibility(child: Text('Selected',style: TextStyle(
                            color: Colors.white,
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w400
                        )),visible: cityId == item['id'].toString(),),
                        SizedBox(width: 6.w,),
                        Image.asset('assets/common/control_right.png',width: 48.w,)
                      ],
                    ),
                  ),
                );
              },itemCount: states.length,padding: EdgeInsets.zero,),
            )),
            SizedBox(height: ScreenUtil().bottomBarHeight,)
          ],
        ),
      ),
    );
  }

}
